require 'open-uri'
class MoviesController < ApplicationController
  def initialize
    @model=Movie
    @name=:movie
 end
 

def import_list
    blob  = open('http://www.azillionmonkeys.com/qed/imdbtop.html') {|f| f.read }
    blob.gsub!("\n", "")
    blob.gsub!("\r", "")
    rows = blob.scan(/<tr>.*?<\/tr>/)
    counter=0
    session[:progress]=0
    rows.each{|row|
       props = row.scan(/<td>(\d+)<\/td><td class="rate">([\d\.]+)<\/td><td>(.*?)<\/td><td class="qty" align=right>(\d+)&nbsp;<\/td><td><a href="bounce.html\?(.*?)" target="imdb">(.*?)<\/a><\/td>/)
       if props and props[0] and props[0].length>5
      props=props[0]
      m=Movie.new
      m.rank=props[0]
      m.raiting=props[1]
      if(props[2].include(img))

      else
       props = row.scan(/.*?a href="(.*?)" .*?><.*>/)
        m.text=props[2]
      end
      m.seen_by=props[3]
      m.url=props[4]
      m.name=props[5]
#      m.release=props[6]
      m.save
    counter+=1
       else
         print"\n\n"
         print row

       end
    if(counter % 30 == 0)
      session[:progress] += 0.1
      session.update(:timeout=>60)
    end
 
    }
       print counter
  render :text=> "<p> Progress: <%= (session[:progress] * 100).to_i %>%</p>#{counter}"
end

def delete_all
  ms=@model.all
  ms.each{|m| m.destroy}
  render :text=>"everything deleted"
end
def import_js

end
def import_imdb

end
def wish_list
@records = Movie.first(100,:conditions =>{:seen=>true})
#@records.get(params[:id])
#render :partial=>'/wish_list'
end

def seen
  @record = Movie.find(params[:id])
  @record.seen=true
  @record.save
  print "xxxx"
  render :text=>"ok"
end


end

